package com.dong.service.impl;

import com.dong.dao.VideoMapper;
import com.dong.entitys.Video;
import com.dong.entitys.Video1;
import com.dong.service.VideoService;
import com.dong.tools.DBTools;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import javax.xml.ws.Action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VideoServiceImpl implements VideoService {

    @Override
    public Video selectVieoById(Integer id) {
        SqlSession sqlSession = DBTools.getSession();
        VideoMapper mapper = sqlSession.getMapper(VideoMapper.class);
        Video video = mapper.selectVideoById(id);
        sqlSession.commit();
        sqlSession.close();
        return video;
    }

    @Override
    public Integer addVideo(Video video) {
        SqlSession session = DBTools.getSession();
        VideoMapper mapper = session.getMapper(VideoMapper.class);
        Integer integer = mapper.addVideo(video);
        session.commit();
        session.close();
        return integer;
    }

    @Override
    public Integer updateVideo(Map map) {
        SqlSession session = DBTools.getSession();
        VideoMapper mapper = session.getMapper(VideoMapper.class);
        Integer integer = mapper.updateVideo(map);
        session.commit();
        session.close();
        return integer;
    }

    @Override
    public PageInfo videoByPage(Integer currentPage, Integer pageSize) {
        SqlSession session = DBTools.getSession();
        VideoMapper mapper = session.getMapper(VideoMapper.class);
         PageHelper.startPage(currentPage, pageSize);
        List<Video> list = mapper.selectAllVideo();
        PageInfo<Video> pageInfo = new PageInfo<>(list);
        session.commit();
        session.close();
        return pageInfo;
    }



    @Override
    public List<Video> selectVideosByConndition(Video video) {
        SqlSession session = DBTools.getSession();
        VideoMapper mapper = session.getMapper(VideoMapper.class);
        List<Video> list = mapper.selectVideosByConndition(video);
        session.commit();
        session.close();
        return list;
    }

    @Override
    public Integer deleteVideo(Integer id) {
        SqlSession session = DBTools.getSession();
        VideoMapper mapper = session.getMapper(VideoMapper.class);
        Integer integer = mapper.deleteVideo(id);
        session.commit();
        session.close();
        return integer;
    }

    @Override
    public List<Video> selectAllVideo() {
        SqlSession sqlSession = DBTools.getSession();
        VideoMapper videoMapper = sqlSession.getMapper(VideoMapper.class);
        List<Video> list = videoMapper.selectAllVideo();
        sqlSession.commit();
        sqlSession.close();
        return list;
    }

}
